package job

import (
	"fmt"
	"strconv"
	"ylb/entity"
	"ylb/mapper"
	"ylb/service"
	"ylb/service/impl"
)

type JobCron struct {
}

func (j JobCron) Run() {
	var email service.EmailService = impl.EmailServiceImpl{}
	vanceMapper := mapper.NewUserReleVanceMapper()
	var jobservice service.JobService = impl.NewJobServiceImpl()
	withEmail := vanceMapper.SelectAllRelevanceWithEmail()
	fmt.Printf("Job邮件提示人数: %v\n", len(withEmail))

	for i := range withEmail {
		id := withEmail[i].UserId
		expire := jobservice.SelectAllByDayExpire(id, 3)
		if len(expire) == 0 {
			continue
		}
		ans := ""
		//模板css样式
		css := `
		 <style>

			.row0{
				background-color: white;
			}
			.row1{
				background-color: #dad9d9;
			}

    	</style>
`
		//模板表格内容
		ans += css
		ans = `
					<table border cellspacing="0" align="center">
					<tr>
						<td style="color:tomato"> 任务名称 </td>
						<td style="color:tomato">过期时间</td>
					</tr>`

		//渲染数据
		for i, item := range expire {
			ans += ("<tr class='row" + strconv.Itoa(i%2) + "' ><td>" + item.Name + " </td><td> " + item.Expire.Format("2006/01/02 15:04:05") + "</td></tr>")
		}
		ans += "</table>"
		fmt.Printf("发送消息 :\n %v\n", ans)

		err := email.SendHtml(entity.Email{
			Subject: "YNote过期提示",
			Body:    "下列任务即将过期:<br> \n\n" + ans,
			Receiver: []string{
				withEmail[i].Email,
			},
		})
		if err != nil {
			fmt.Printf("%v\n", "发送失败")

		}

	}
}
